﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;
using CsharpTrainer.NHibernate3;
using CsharpTrainer.NHibernate3.Mapping;
using CsharpTrainer.NHibernate3.Entities;
using Zeus.Thunder.Core;
using NHibernate;

namespace SharpTrainer.NHibernate
{
    class QueryCategoryArticleTest : ITestCase
    {
        public void Run()
        {
            string connStr = ConfigurationManager.ConnectionStrings["NH3TrainConnStr"].ConnectionString;

            try
            {
                NHibernateDb<ArticleMap> db = new NHibernateDb<ArticleMap>(connStr);
                
                ISessionFactory factory = db.SessionFactory;
                using (var session = factory.OpenSession())
                {
                    var categories = session.QueryOver<Category>()
                        .OrderBy(c => c.Name).Asc
                        .List();

                    for (int i = 0; i < categories.Count; i++)
                        Console.WriteLine(categories[i].Name);

                    Console.Write("Please Input the ID of the Category: ");
                    int id = Convert.ToInt32(Console.ReadLine());
                    var category = session.Get<Category>(id);

                    Console.WriteLine(category.Name);
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine("Exception: " + ex.ToString());
            }
        }
    }
}
